<?php

//王晓梦
declare (strict_types=1);
namespace app\backend\controller\v1;
use app\backend\validate\DriverValidate;
use app\BaseController;
use think\exception\ValidateException;
use think\Request;
use app\common\model\Driver as DriverModel;
use app\common\model\Cate;

class Driver extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $type = input('type', 2);
        //获取分类id
        $cate_id = Cate::where('type', $type)->column('id');
        $page = input('page/d', 1);
        $size = 10;
        $rows = count(DriverModel::where('status', 1)->select()->toArray());
        $maxPage = ceil($rows / $size);
        $datas = DriverModel::where(['status' => 1])
            ->where('cate_id', 'in', $cate_id)
            ->with(['getCate'])
            ->order('id desc')
            ->limit(($page - 1) * 10, 10)
            ->select();
        foreach ($datas as &$v){
            if (!empty($v['image']))
                $v['image'] = env('app.yuming') . $v['image'];
            $v['cate_id'] = $v->getCate->name;
            unset($v->getCate);
        }

        return sucessMsg(200, '获取成功！', ['data' => $datas, 'page' => $page, 'maxPage' => $maxPage, 'rows' => $rows]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $datas = getList(Cate::where(['status' => 1, 'type' => 2])->field('id,name,pid')->select()->toArray());
        return sucessMsg(200, '获取成功！', $datas);
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //添加产品
        $postData = input();
        try {
            //  验证参数
            validate(DriverValidate::class)->scene('save')->check($postData);
            $model = new DriverModel();
            if ($model->save($postData))
                return sucessMsg(200, '添加成功');
            return sucessMsg(422, '添加失败');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @return \think\Response
     */
    public function edit()
    {
        $id = input('get.id/d');
        $model = new DriverModel();
        if (empty($id))
            return errMsg(422, 'id不能为空');
        $data = $model->where('id', $id)->find();
        $data['image'] = env('app.yuming') . $data['image'];
        $type = Cate::where('id', $data['cate_id'])->column('type');
        $datas = getList(Cate::where(['status' => 1, 'type' => $type[0]])->field('id,name,pid')->select()->toArray());
        if (empty($data))
            return errMsg(422, '无效id');
        return sucessMsg(200, '获取成功！', ['data' => $data,'cate'=>$datas]);
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        //修改
        $postData = input('post.');
        try {
            //  验证参数
            validate(DriverValidate::class)->scene('update')->check($postData);
            $model = new DriverModel();
            $cate = $model->where('id', $postData['id'])->find();
            if ($cate->save($postData))
                return sucessMsg(200, '修改成功');
            return sucessMsg(422, '修改失败');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d');
        if(empty($id)){
            return errMsg(422, 'id不能为空');
        }
        $data = DriverModel::where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        if (DriverModel::destroy($id))
            return sucessMsg(200, '删除成功!');
        return errMsg(422, '删除失败');
    }
}
